/**
 * @program: LeetCode
 * @description: LeetCode : 剑指 Offer II 008. 和大于等于 target 的最短子数组
 * @author: WXY
 * @create: 2022-12-21 13:56
 * @Version 1.0
 **/

public class offer_three2_minSubArrayLen {
    public static int minSubArrayLen(int target, int[] arr) {
        int left = 0;
        int right = 0;
        int sum = 0;
        int ans = Integer.MAX_VALUE;
        while (right < arr.length) {
            sum += arr[right++];
            while (sum >= target) {
                ans = Math.min(ans, right - left);
                sum -= arr[left++];
            }
        }
        return ans == Integer.MAX_VALUE ? 0 : ans;
    }

}
